package com.example.orderassembly.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.orderassembly.service.IScoreOrderService;
import com.woniu.cloud.common.exception.TicketException;
import com.woniuxy.api.ScoreOrderApi;
import com.woniu.cloud.common.dto.Result;
import com.woniuxy.entity.ScoreOrder;
import com.woniuxy.form.GetOrder;
import com.woniuxy.form.InsertScoreOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分订单表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@RestController
@Slf4j
@SuppressWarnings("all")
public class ScoreOrderController implements ScoreOrderApi {
    @Resource
    IScoreOrderService scoreOrderService;

    /**
     * 已测
     * @param getOrder  查询条件
     * @return
     */
    @Override
    public Result getScoreOrderByUserId(GetOrder getOrder) {
        log.info("根据用户Id 分页查询用户积分兑换记录 参数 getOrder={}",getOrder);
        if (null==getOrder){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        IPage<ScoreOrder> iPage = scoreOrderService.getScoreOrderByUserId(getOrder);
        return iPage.getRecords().size()==0?Result.fail("查询失败",iPage):Result.success("成功",iPage);
    }

    /**
     * 已测
     * @param insertScoreOrder  用户根据积分兑换商品生成商品订单
     * @return
     */
    @PostMapping("/insertScoreOrder")
    @Override
    public Result insertScoreOrder(InsertScoreOrder insertScoreOrder) {
        log.info("新增用户积分订单 参数 insertScoreOrder={}",insertScoreOrder);
        if(null==insertScoreOrder){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Boolean bool = scoreOrderService.insertScoreOrder(insertScoreOrder);
        return bool?Result.success("新增订单成功",bool):Result.fail("失败",bool);
    }

    @Override
    public Result updateScoreOrderStatus(Integer scoreGoodId, Integer status) {
        log.info("修改积分商品订单状态 scoreGoodId={},status={}",scoreGoodId,status);
        if(scoreGoodId==null||status==null){
            log.info("参数错误");
//            return Result.fail("参数错误",null);
            throw  new TicketException("参数异常");
        }
        Boolean bool = scoreOrderService.updateScoreOrderStatus(scoreGoodId,status);
        return bool?Result.success("修改订单成功",bool):Result.fail("失败",bool);
    }
}

